

local CustomClock = CustomClock;




function CustomClock:Config_LoadSettings()
	local config = CustomClock_Config;
	
	if (config == nil) then
		config = {
			TextRed = 1.0;
			TextGreen = 1;
			TextBlue = 1;
			TextSize = 60;
			FontPath = ("Fonts\\FRIZQT__.TTF");
			TimePattern = ("%I:%M:%S %p");
			FrameWidth = 200;
			FrameHeight = 40;
			FrameOffsetX = -2;--(UIParent:GetWidth() / 2.0) ;
			FrameOffsetY = 6;--(UIParent:GetHeight() / 2.0);
		};
	end
	
	
	local frame = self.DISPLAY_FRAME;
	
	frame:SetWidth(config.FrameWidth);
	frame:SetHeight(config.FrameHeight);
	frame:SetPoint("topleft", "UIParent", "topLEFT", config.FrameOffsetX, config.FrameOffsetY);
	
	frame.TEXT:SetFont(config.FontPath, config.TextSize);
	frame.TEXT:SetTextColor(config.TextRed, config.TextGreen, config.TextBlue);
	
	frame:Show();
	
	
	self.Config = config;
end




function CustomClock:Config_SaveSettings()
	local mathfloor = math.floor;
	
	local config = self.Config;
	local frame = self.DISPLAY_FRAME;
	
	
	config.FrameWidth = mathfloor(frame:GetWidth());
	config.FrameHeight = mathfloor(frame:GetHeight());
	
	local x, y = frame:GetCenter();
	config.FrameOffsetX = mathfloor(x);
	config.FrameOffsetY = mathfloor(y);
	frame:SetUserPlaced(false);
	
	
	local fontPath, fontHeight = frame.TEXT:GetFont();
	config.FontPath = fontPath;
	config.TextSize = mathfloor(fontHeight);
	
	config.TextRed, config.TextGreen, config.TextBlue = frame.TEXT:GetTextColor();
end




function CustomClock:Config_SetDisplayPattern(pattern)
	local valid, dateString = pcall(date, pattern);
	
	if (valid == true and type(dateString) == "string") then
		self.TimePattern = pattern;
		return pattern;
	end
	
	return nil;
end




function CustomClock:Config_SetTextColor(red, green, blue)
	if (
	  red ~= nil and red >= 0 and red <= 100
	  and green ~= nil and green >= 0 and green <= 100
	  and blue ~= nil and blue >= 0 and blue <= 100
	) then
		red = (red / 100);
		green = (green / 100);
		blue = (blue / 100);
		
		self.Config.TextRed = red;
		self.Config.TextGreen = green;
		self.Config.TextBlue = blue;
		
		self.DISPLAY_FRAME.TEXT:SetTextColor(red, green, blue);
		
		return true;
	end
	
	
	return false;
end




function CustomClock:Config_SetFontPath(newPath)
	local text = self.DISPLAY_FRAME.TEXT;
	local oldPath, size, flags = text:GetFont();
	
	text:SetFont(newPath, size, flags);
	
	
	if (text:GetFont():lower() == newPath:lower()) then
		self.Config.FontPath = newPath;
		return true;
	end
	
	
	text:SetFont(oldPath, size, flags); -- Restore the old font to avoid display corruption.
	
	return false;
end




function CustomClock:Config_SetFontSize(newSize)
	local text = self.DISPLAY_FRAME.TEXT;
	local path, size, flags = text:GetFont();
	
	
	if (pcall(text.SetFont, text, path, newSize, flags) == true) then
		self.Config.TextSize = newSize;
		return true;
	end
	
	
	text:SetFont(path, size, flags); -- Restore the old size to avoid display corruption.
	
	return false;
end




function CustomClock:Config_SetFrameWidth(width)
	width = tonumber(width);
	
	if (width ~= nil and width > 0 and width <= UIParent:GetWidth()) then
		self.DISPLAY_FRAME:SetWidth(width);
		self.Config.FrameWidth = width;
		return true;
	end
	
	return false;
end




function CustomClock:Config_SetFrameHeight(height)
	height = tonumber(height);
	
	if (height ~= nil and height > 0 and height <= UIParent:GetHeight()) then
		self.DISPLAY_FRAME:SetHeight(height);
		self.Config.FrameHeight = height;
		return true;
	end
	
	return false;
end




CustomClock.DISPLAY_FRAME:SetScript("OnMouseDown",
  function(self)
	if (IsModifierKeyDown() ~= nil) then
		self.Moving = true;
		self:StartMoving();
	end
  end
);



CustomClock.DISPLAY_FRAME:SetScript("OnMouseUp",
  function(self)
	if (self.Moving ~= nil) then
		self.Moving = nil;
		self:StopMovingOrSizing();
	end
  end
);

